import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/commom/Dio.dart';
import 'package:flutter_app/commom/Global.dart';
import 'package:flutter_app/models/ganttChart.dart';
import 'package:flutter_app/models/login.dart';
import 'package:flutter_app/widgets/AplResultCard.dart';
import 'package:flutter_app/widgets/HYSizeFit.dart';

import 'GanttRoute.dart';

class ProjectInfo extends StatefulWidget{
  ProjectInfo({
    Key key,
    @required this.index,
    @required this.flag,
  }) : super(key: key);
  final int index;
  final int flag;
  @override
  State<StatefulWidget> createState() {
    return ProjectInfoState(index, flag);
  }
}

class ProjectInfoState extends State<ProjectInfo> {
  final int index;
  final int flag;
  int projectId = 0;
  ProjectInfoState(this.index, this.flag);
  GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  @override
  void initState() {}

  Widget pdInfoItem(BuildContext context, int position) {
    return Container(
      height: HYSizeFit.setRpx(215),
      padding: EdgeInsets.only(top:HYSizeFit.setPx(10), bottom: HYSizeFit.setPx(0)),
      color: Colors.white,
      //margin: EdgeInsets.all(5.0),
      child: Padding(
        padding: EdgeInsets.only(bottom: HYSizeFit.setPx(0),left: HYSizeFit.setPx(5), right: HYSizeFit.setPx(5)),
        child: Column(
          crossAxisAlignment:CrossAxisAlignment.start,
          children: [
            Flex(
              direction: Axis.horizontal,
              crossAxisAlignment:CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Icon(
                  Icons.paste_rounded,
                  size: HYSizeFit.setPx(25),
                ),
                Text(
                    Global.matchingFriend.projects[index]['project']['procedures'][position]['pdName'],
                    overflow: TextOverflow.ellipsis,
                    softWrap: true,
                    style: TextStyle(
                        color:Colors.black54,
                        fontSize: HYSizeFit.setPx(17),
                        fontWeight: FontWeight.w700
                    )
                ),
                Column(
                  crossAxisAlignment:CrossAxisAlignment.start,
                  mainAxisAlignment:MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '开始：' + formatDate(Global.matchingFriend.projects[index]['project']['procedures'][position]['pdProtime'], [yyyy, '-', mm, '-', dd]),
                      style: TextStyle(
                        color:Colors.black87,
                        fontSize: HYSizeFit.setPx(12),
                        //fontWeight: FontWeight.w700
                      ),
                    ),
                    Padding(padding: EdgeInsets.only(top:HYSizeFit.setPx(5))),
                    Text(
                      '交期：' + formatDate(Global.matchingFriend.projects[index]['project']['procedures'][position]['pdDeltime'], [yyyy, '-', mm, '-', dd]),
                      style: TextStyle(
                        color:Colors.black87,
                        fontSize: HYSizeFit.setPx(12),
                        //fontWeight: FontWeight.w700
                      ),
                    ),
                    Padding(padding: EdgeInsets.only(top:HYSizeFit.setPx(5))),
                    Text(
                      '数量：' + Global.matchingFriend.projects[index]['project']['procedures'][position]['pdDemand'].toString(),
                      style: TextStyle(
                        color:Colors.black87,
                        fontSize: HYSizeFit.setPx(12),
                        //fontWeight: FontWeight.w700
                      ),
                    ),
                  ],
                ),
                Column(
                  mainAxisAlignment:MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '状态：' + Global.matchingFriend.projects[index]['project']['procedures'][position]['pdStatusTxt'],
                      style: TextStyle(
                        color:Colors.black87,
                        fontSize: HYSizeFit.setPx(15),
                        //fontWeight: FontWeight.w700
                      ),
                    ),
                    Container(
                      height: HYSizeFit.setPx(25),
                      padding: EdgeInsets.only(top: HYSizeFit.setPx(5)),
                      child: OutlineButton(
                          borderSide: BorderSide(
                            color: Colors.black26,
                          ),
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(HYSizeFit.setPx(20))
                          ),
                          child: Text(
                            '发送包揽申请',
                            style: TextStyle(
                                color: flag != 0 ? Colors.black87:Colors.black26,
                                fontSize: HYSizeFit.setPx(12)
                            ),
                          ),
                          onPressed: ()=>{
                            flag != 0 ?_onUndertake(Global.matchingFriend.projects[index]['owner']['usrId'], Global.matchingFriend.projects[index]['project']['pjId'], Global.matchingFriend.projects[index]['project']['procedures'][position]['pdId']) : null
                          }
                      ),
                    )
                  ],
                ),
              ],
            ),
            Padding(
              padding: EdgeInsets.only(top: HYSizeFit.setPx(5)),
              child: Container(
                height: HYSizeFit.setPx(20),
                  decoration: new BoxDecoration(
                    //border: new Border.all(color: Colors.black12, width: 0.5), // 边色与边宽度
                    color: Colors.black12, // 底色
                    borderRadius: BorderRadius.all(Radius.circular(HYSizeFit.setPx(10))), // 也可控件一边圆角大小
                    //borderRadius: new BorderRadius.vertical(top: Radius.elliptical(20, 60)),
                  ),
                  child: Padding(
                    padding: EdgeInsets.only(right: HYSizeFit.setPx(5), left: HYSizeFit.setPx(5)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text(
                          '业务范围：' + Global.matchingFriend.projects[index]['project']['procedures'][position]['show'],
                          overflow: TextOverflow.ellipsis,
                          softWrap: true,
                          style: TextStyle(
                            color:Colors.black87,
                            fontSize: HYSizeFit.setPx(13),
                            //fontWeight: FontWeight.w700
                          ),
                        ),
                      ],
                    ),
                  )
              ),
            ),
          ],
        )
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    HYSizeFit.initialize(context);
    return Scaffold(
      backgroundColor: Color.fromRGBO(239, 239, 239, 1),
      appBar: AppBar(
          backgroundColor: Colors.white,
          centerTitle: true,
          title: Text('订单详情',
              textAlign: TextAlign.center,
              style: TextStyle(
                //color: Colors.grey[700],
                fontSize: HYSizeFit.setPx(19),
                height: 1.2,
                //fontFamily: "Microsoft YaHei UI",
                //decorationThickness: 1.0,
                //fontWeight: FontWeight.w900,
              )),
          elevation: 0, //去掉Appbar底部阴影
          brightness: Brightness.light, //状态栏颜色
          leading: Builder(builder: (context) {
            return new IconButton(
                icon: new Icon(
                  Icons.arrow_back_ios,
                  color: Colors.grey,
                ),
                tooltip: '退出',
                onPressed: () => {
                  Navigator.pop(context)
                });
          })),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.black54,
        mini: true,
        child: Icon(
            Icons.bar_chart
        ),
        onPressed: (){
          _onGanttChart(context);
        },
        heroTag: index,
      ),
      body: Column(
        //mainAxisAlignment:MainAxisAlignment.spaceAround,
        children: <Widget>[
          //工程详情
          Padding(
            padding: EdgeInsets.fromLTRB(10,5,10,5),
            child: Card(
              elevation: 2.0,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(20.0)),
              ),
              child: Padding(
                padding: EdgeInsets.fromLTRB(HYSizeFit.setPx(15),HYSizeFit.setPx(10),HYSizeFit.setPx(15),HYSizeFit.setPx(10)),
                child: Flex(
                  direction: Axis.vertical,
                  crossAxisAlignment:CrossAxisAlignment.start,
                  children: [
                    Text(
                      "工程详情",
                      style: TextStyle(
                        color: Colors.black87,
                        fontSize: HYSizeFit.setPx(15),
                      ),
                    ),
                    Column(
                      crossAxisAlignment:CrossAxisAlignment.end,
                      children: [
                        Text(
                          Global.matchingFriend.projects[index]['project']['pjName'],
                          style: TextStyle(
                            color: Colors.black54,
                            fontSize: HYSizeFit.setPx(20),
                            //height: 1.2
                            fontWeight: FontWeight.w700
                          ),
                        ),
                        Padding(padding: EdgeInsets.only(top: HYSizeFit.setPx(10))),
                        Row(
                          crossAxisAlignment:CrossAxisAlignment.end,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              "发布日期：" + formatDate(Global.matchingFriend.projects[index]['project']['pjReltime'], [yyyy, '-', mm, '-', dd]),
                              style: TextStyle(
                                color: Colors.black87,
                                fontSize: HYSizeFit.setPx(12),
                              ),
                            ),
                            Padding(padding: EdgeInsets.only(right: HYSizeFit.setPx(10))),
                            Text(
                              "交期：" + formatDate(Global.matchingFriend.projects[index]['project']['pjDeltime'], [yyyy, '-', mm, '-', dd]),
                              style: TextStyle(
                                color: Colors.black87,
                                fontSize: HYSizeFit.setPx(12),
                              ),
                            ),
                          ],
                        )
                      ],
                    )
                  ],
                )
              ),
            ),
          ),
          //工序列表
          Padding(
            padding: EdgeInsets.fromLTRB(HYSizeFit.setPx(10),HYSizeFit.setPx(5),HYSizeFit.setPx(10),HYSizeFit.setPx(5)),
            child: Container(
              height: HYSizeFit.setRpx(640),
              width: HYSizeFit.screenWidth,
              child: Card(
                elevation: 2.0,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(HYSizeFit.setPx(20))),
                ),
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0,HYSizeFit.setPx(10),0,HYSizeFit.setPx(10)),
                  child: Column(
                    crossAxisAlignment:CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(left: HYSizeFit.setPx(15)),
                        child: Text(
                          "工序列表",
                          style: TextStyle(
                            color: Colors.black87,
                            fontSize: HYSizeFit.setPx(15),
                          ),
                        ),
                      ),
                      Container(
                          height: HYSizeFit.setRpx(530),
                          width: HYSizeFit.screenWidth,
                          child:ListView.separated(
                            itemCount: (Global.matchingFriend.projects[index]['project']['procedures'] == null)? 0 : Global.matchingFriend.projects[index]['project']['procedures'].length,
                            itemBuilder: pdInfoItem,
                            shrinkWrap: true,
                            separatorBuilder: (BuildContext context, int index){
                              return Divider(
                                color: Colors.black12,
                                height: HYSizeFit.setRpx(0.5),
                                indent: HYSizeFit.setRpx(60),
                                endIndent: HYSizeFit.setRpx(60),
                              );
                            },
                          )
                      )
                    ],
                  ),
                ),
              ),
            )
          ),
          //用户详情
          Padding(
              padding: EdgeInsets.fromLTRB(HYSizeFit.setPx(10),5,HYSizeFit.setPx(10),5),
              child: Container(
                //height: HYSizeFit.setRpx(400),
                width: HYSizeFit.screenWidth,
                child: Card(
                  elevation: 2.0,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(HYSizeFit.setPx(20))),
                  ),
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(0,HYSizeFit.setPx(10),0,HYSizeFit.setPx(10)),
                    child: Flex(
                      direction: Axis.vertical,
                      crossAxisAlignment:CrossAxisAlignment.start,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(left: HYSizeFit.setPx(15)),
                          child: Text(
                            "需求方详情",
                            style: TextStyle(
                              color: Colors.black87,
                              fontSize: HYSizeFit.setPx(15),
                            ),
                          ),
                        ),
                        Container(
                            padding: EdgeInsets.all(HYSizeFit.setPx(12)),
                            //height: HYSizeFit.setRpx(200),
                            width: HYSizeFit.screenWidth,
                            child: Flex(
                              direction: Axis.horizontal,
                              children: <Widget>[
                                Expanded(
                                  flex: 2,
                                  child: ClipRect(
                                    child: Image.asset(
                                      "imgs/企业头像.jpeg",
                                      width: HYSizeFit.setPx(50),
                                    ),
                                  ),
                                ),
                                Expanded(
                                    flex: 7,
                                    child: Padding(
                                      padding: EdgeInsets.only(left: HYSizeFit.setPx(20)),
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                                        children: [
                                          RichText(text: TextSpan(
                                              style: TextStyle(
                                                fontSize: HYSizeFit.setPx(23),
                                                color: Colors.black,
                                              ),
                                              children: [
                                                TextSpan(
                                                  //var name = FriendList[position]['usrName'];
                                                  text: Global.matchingFriend.projects[index]['owner']['usrName'],
                                                ),
                                                WidgetSpan(child: Padding(
                                                  padding: const EdgeInsets.symmetric(horizontal: 2.0),
                                                  child: Icon(Icons.person),
                                                ))

                                              ]
                                          )
                                          ),
                                          Text(
                                            "帮号：" + Global.matchingFriend.projects[index]['owner']['usrId'].toString(),
                                            style: TextStyle(
                                              fontSize: HYSizeFit.setPx(13),
                                              color: Colors.grey[700],
                                            ),
                                          ),
                                          Text(
                                            "电话：" + Global.matchingFriend.projects[index]['owner']['usrTel'],
                                            style: TextStyle(
                                              fontSize: HYSizeFit.setPx(13),
                                              color: Colors.grey[700],
                                            ),
                                          ),
                                        ],
                                      ),
                                    )),

                              ],
                            ),
                        ),
                        Divider(
                          color: Colors.black87,
                          height: HYSizeFit.setRpx(30),
                          indent: HYSizeFit.setRpx(25),
                          endIndent: HYSizeFit.setRpx(25),
                        ),
                        GestureDetector(
                            onTap: () async {
                              //await showSucceedConfirmDialog();
                            },
                            child: Card(
                                elevation: 0,
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Icon(flag != 0?Icons.messenger_rounded: Icons.person_add),
                                    Text(
                                      flag != 0? "   发送消息" : "   添加到好友列表",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        fontSize: HYSizeFit.setPx(18),
                                        color: Colors.black,
                                      ),
                                    ),
                                  ],
                                )))
                      ],
                    ),
                  ),
                ),
              )
          ),
        ],
      ),
    );
  }
  void _onGanttChart(BuildContext context){
      Global.usersInChart = List();
      Global.dataInChart = List();
      Global.matchingFriend.projects[index]['project']['procedures'].forEach((element) {
        print("");
        if(element['pdStatus'] == 400){
          Global.dataInChart.add(
              Project(
                  id:projectId,
                  name:element['pdName'],
                  startTime:element['pdProtime'],
                  endTime: element['pdDeltime'],
                  participants: [0]
              )
          );
        }
        else{
          Global.dataInChart.add(
              Project(
                  id:projectId,
                  name:element['pdName'],
                  startTime:element['pdProtime'],
                  endTime: element['pdDeltime'],
                  participants: [1]
              )
          );
        }
        projectId = projectId + 1;
      });
      Global.usersInChart.add(
          User(
              id: 0,
              name: '待包揽'
          )
      );
      Global.usersInChart.add(
          User(
              id: 1,
              name: '已包揽'
          )
      );
      Navigator.push(
        context,
        new MaterialPageRoute(builder: (context) {
          return new GanttChartScreen(
              projects:Global.dataInChart,
              users:Global.usersInChart,
              title: Global.matchingFriend.projects[index]['project']['pjName'] + "-甘特图",
              fromDate: Global.matchingFriend.projects[index]['project']['pjProtime'],
              toDate: Global.matchingFriend.projects[index]['project']['pjDeltime'],
          );
        }),
      );
  }
  void _onUndertake(int targetId, int pjId, int pdId) async{
    var status = await EA(context).applyUndertake(targetId, pjId, pdId);
    //print(status);
    if(status == 200){
      await AplSuccessedCard(context);
    }
    else await UnderAppFailedCard(context);
  }
}